package com.wsd.web;

import com.alibaba.fastjson.JSONObject;
import com.cif.domain.Customer;
import com.cif.service.CustomerService;
import com.wsd.domain.NormalLoan;
import com.wsd.service.NormalLoanService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname NormalLoanController
 * @Description TODO
 * @Date 2020/7/20 10:03
 * @Created by niuxiaojuan
 */
@RestController
@RequestMapping("/manage")
public class NormalLoanController extends BaseController{
    @Autowired
    NormalLoanService normalLoanService;

    @Reference(version = "1.0.0", url = "${dubbo.service.cifurl}")
    private CustomerService customerService;


    @RequestMapping("/getNormalLoanCount")
    public Map NormalLoanCount()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BigDecimal loanMoney = normalLoanService.sumNormalLoan();//正常贷款总金额
            BigDecimal repayLoanMoney = normalLoanService.sumRepayLoanMoney();//正常贷款已还本金总额
            BigDecimal interest = normalLoanService.findNormalInterest();//正常贷款应收总利息
            BigDecimal repayInterest = normalLoanService.sumRepayInterest();
            map.put("loanMoney",loanMoney);
            map.put("repayLoanMoney",repayLoanMoney);
            map.put("interest",interest);
            map.put("repayInterest",repayInterest);

            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");

        }catch(Exception e)
        {
            map.put("success","false");
            map.put("resultCode", "LOANCOUNT-ERROR");
            map.put("resultMsg","返回正常贷款统计信息出错");
        }
        return map;
    }

    @RequestMapping("/getNormalLoanCountList")
    public Map getNormalLoanCountList(@RequestParam("pagenum") int pagenum, @RequestParam("num") int num)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<NormalLoan> list = normalLoanService.findNormalLoanForm(pagenum, num);
            for (NormalLoan l : list) {
                System.out.println(l.getUserId().toString());
                Customer c = JSONObject.parseObject(JSONObject.toJSONString(changeResult(customerService.getCustomerById(l.getUserId().toString())).getData()),Customer.class);
                l.setUserName(c.getUserName());
                l.setIdNumber(c.getIdNumber());
            }
            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");
            map.put("recordCount",normalLoanService.countNormalLoanForm());
            map.put("list", list);
    }catch(Exception e)
    {
        map.put("success","false");
        map.put("resultCode", "GETNORMALLOAN-ERROR");
        map.put("resultMsg","返回正常贷款信息出错");
    }
        return map;
    }
}
